var DropTarget = function(target, options) { 
	target.addEventListener("dragenter", this.delegate(this, this.dragEnter), false);
	target.addEventListener("dragexit", this.delegate(this, this.dragExit), false);
	target.addEventListener("dragover", this.delegate(this, this.dragOver), false);
	target.addEventListener("drop", this.delegate(this, this.drop), false);
	this.target = target;
	this.options = options;
}
DropTarget.prototype = {
	delegate: function( obj, fn ) {
		return function() { return fn.apply(obj, arguments); }
	},
	dragEnter: function(e) {
		e.stopPropagation();
		e.preventDefault();	
	},
	dragExit: function(e) {
		e.stopPropagation();
		e.preventDefault();	
	},
	dragOver: function(e) {
		e.stopPropagation();
		e.preventDefault();	
	},
	drop: function(e) {
		e.stopPropagation();
		e.preventDefault(); 
	
		var files = e.dataTransfer.files;
		var count = files.length;
	
		// Only call the handler if 1 or more files was dropped.
		if (count > 0) {
			this.handleFiles(files);
		}
	},
	handleFiles: function(files) {
		var self = this,
			file = files[0];
		this.reader = new FileReader();
		this.target.innerHTML = "Processing " + file.name;  
		this.reader.onprogress = this.delegate(this, this.handleReaderProgress);
		this.reader.onloadend = this.delegate(this, this.handleReaderLoadEnd); 
		this.read(file);
		
	},
	handleReaderProgress: function(e) {
		if (e.lengthComputable) {
			var loaded = (e.loaded / e.total); 
			//$("#progressbar").progressbar({ value: loaded * 100 });
		}
	},
	handleReaderLoadEnd: function(e) {
		//$("#progressbar").progressbar({ value: 100 });  
		this.display(e.target.result)
	},
	read: function(file) {
		this.reader.readAsText(file);	
	},
	display: function(result) {
		this.target.innerHTML = result; 
		if (this.options && this.options.onDisplay) {
			this.options.onDisplay.call(this.target);
		}
	}
}